package com.zr.Inquiry.service.impl;

import com.zr.Inquiry.mapper.InquiryMapper;
import com.zr.Inquiry.pojo.*;
import com.zr.Inquiry.service.InquiryService;
import com.zr.planclass.pojo.PlanClassAddVo;
import com.zr.planclass.pojo.PlanClassVo;
import com.zr.plant.pojo.LegalPersonVo;
import com.zr.plant.pojo.PlantVo;
import com.zr.utils.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.*;

import static javafx.scene.input.KeyCode.E;
import static javafx.scene.input.KeyCode.O;

/**
 * @ClassName InquiryServiceImpl
 * @Description TODO
 * @Author xiaowei
 * @Date 2019/12/27 15:10
 * @Version 1.0
 */
@Service
@Transactional
public class InquiryServiceImpl implements InquiryService {
    @Autowired
    private InquiryMapper inquiryMapper;

    //供应商下拉框
    @Override
    public ResultVo queryByLegalPerson(Integer legalPersonId) {
        List<PartnerVo> supplierVoList = inquiryMapper.queryByLegalPerson(legalPersonId);
        if (supplierVoList.size() < 0) {
            return ResultBuilderVo.error(ErrorMessage.OBJECTISNOT);
        }
        return ResultBuilderVo.success(supplierVoList);
    }

    //分页查询
    @Override
    public ResultVo queryPage(InquirySelectVo inquirySelectVo) {
        AllRecords allRecords = new AllRecords();
        //根据条件查询总条数
        int count = inquiryMapper.queryCount(inquirySelectVo);
        //根据条件查询数据
        List<InquiryTotalVo> inquiryVoList = inquiryMapper.queryData(inquirySelectVo);
        for (InquiryTotalVo inquiryVo : inquiryVoList) {
                inquiryVo.setInquiryStatusName(InquiryStatusEnum.getName(inquiryVo.getInquiryStatus()));
        }
        //当前页
        allRecords.setPageIndex(inquirySelectVo.getPageIndex());
        //每页显示条数
        allRecords.setPageSize(inquirySelectVo.getPageSize());
        //总数量
        allRecords.setTotalNumber(count);
        //分页的列表数据
        allRecords.setDataList(inquiryVoList);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    /**
     * 查看
     * @param requestForQuotationId
     * @return
     */
    @Override
    public ResultVo see(String requestForQuotationId) {
        ArrayList<InquiryTotalVo> inquiryTotalVoList = new ArrayList<>();
        InquiryTotalVo inquiryTotalVo=inquiryMapper.queryInquiryByrequestForQuotationId(requestForQuotationId);
        inquiryTotalVo.setCurrencyCodeName(CurrencyCodeEnum.getName(inquiryTotalVo.getCurrencyCode()));
        inquiryTotalVo.setTypeName(TypeEnum.getName(inquiryTotalVo.getType()));
        inquiryTotalVo.setIsTaxName(IsTaxEnum.getName(inquiryTotalVo.getIsTax()));

        List<InquirySpareVo> inquiryDetailsList=inquiryMapper.querySpareByrequestForQuotationId(inquiryTotalVo.getId());
        List<InquirySupplierVo> supplierDetailsList=inquiryMapper.querySupplierByrequestForQuotationId(inquiryTotalVo.getId());
        inquiryTotalVo.setInquiryDetailsList(inquiryDetailsList);
        inquiryTotalVo.setSupplierDetailsList(supplierDetailsList);
        inquiryTotalVoList.add(inquiryTotalVo);
        return ResultBuilderVo.success(inquiryTotalVoList);
    }

    //作废
    @Override
    public ResultVo modifyState(InquiryTotalVo inquiryTotalVo) {
        inquiryMapper.modifyState(inquiryTotalVo);
        return ResultBuilderVo.success();
    }

    //审核
    @Override
    public ResultVo inquiryAuditorUpdate(String requestForQuotationId) {
        inquiryMapper.updateInquiryTotalInquiryStatus(requestForQuotationId);
        return ResultBuilderVo.success();
    }

    //新增
    @Override
    public ResultVo add(InquiryAddandUpdateVo inquiryAddandUpdateVo) {
        ResultVo resultVo = validateInquiry(inquiryAddandUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        ResultVo resultVo1 = validateInquiry2(inquiryAddandUpdateVo);
        if (!resultVo1.getSuccess()){
            return resultVo1;
        }
        ResultVo resultVo2 = validateInquiry3(inquiryAddandUpdateVo);
        if (!resultVo2.getSuccess()){
            return resultVo2;
        }
        ResultVo resultVo3 = validateInquiry4(inquiryAddandUpdateVo);
        if (!resultVo3.getSuccess()){
            return resultVo3;
        }
        InquiryTotalVo inquiryTotal=new InquiryTotalVo();
        Date date=new Date();
        BeanUtils.copyProperties(inquiryAddandUpdateVo,inquiryTotal);
        inquiryTotal.setRequestForQuotationId(UUID.randomUUID().toString());
        inquiryTotal.setCreateTime(date);
        inquiryTotal.setCreatorName("小伟");
        inquiryTotal.setUpdateTime(date);
        inquiryTotal.setUpdateName("小伟");
        inquiryMapper.addInquiryTotal(inquiryTotal);
        int id=inquiryMapper.selectByRequestForQuotationId(inquiryTotal.getRequestForQuotationId());
        //循环遍历询价备件，加进其表
        List<InquirySpareVo> inquirySpareCodeVoList =inquiryAddandUpdateVo.getInquiryDetailsList();
        for (InquirySpareVo inquirySpareCodeVo : inquirySpareCodeVoList){
            inquirySpareCodeVo.setInquiryId(id);
        }
        inquiryMapper.addInquirysparedetail(inquirySpareCodeVoList);
        //循环遍历询价供应商，加进其表
        List<InquirySupplierVo> inquirySupplierVoList =inquiryAddandUpdateVo.getSupplierDetailsList();
        for (InquirySupplierVo inquirySupplierVo : inquirySupplierVoList){
            inquirySupplierVo.setInquiryId(id);
        }
        inquiryMapper.addInquirysupplierdetail(inquirySupplierVoList);
        return ResultBuilderVo.success();
    }

    //修改
    @Override
    public ResultVo update(InquiryAddandUpdateVo inquiryAddandUpdateVo) {
        String requestForQuotationId = inquiryAddandUpdateVo.getRequestForQuotationId();
        if (requestForQuotationId.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.DANHAOISNULL);
        }

        InquiryTotalVo inquiryTotal2 = inquiryMapper.queryByRequestForQuotationId(inquiryAddandUpdateVo.getRequestForQuotationId());
        if (inquiryTotal2==null){
            return ResultBuilderVo.error(ErrorMessage.UPDATEISNULL);
        }
        //业务逻辑验证
        //验证
        ResultVo resultVo = validateInquiry(inquiryAddandUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        ResultVo resultVo1 = validateInquiry2(inquiryAddandUpdateVo);
        if (!resultVo1.getSuccess()){
            return resultVo1;
        }
        ResultVo resultVo2 = validateInquiry3(inquiryAddandUpdateVo);
        if (!resultVo2.getSuccess()){
            return resultVo2;
        }
        ResultVo resultVo3 = validateInquiry4(inquiryAddandUpdateVo);
        if (!resultVo3.getSuccess()){
            return resultVo3;
        }
        //入库
        //入库主表数据
        InquiryTotalVo inquiryTotal = new InquiryTotalVo();
        BeanUtils.copyProperties(inquiryAddandUpdateVo,inquiryTotal);
        Date nowDate = new Date();
        inquiryTotal.setUpdateName("张三");
        inquiryTotal.setUpdateTime(nowDate);
        inquiryMapper.updateInquiryTotal(inquiryTotal);
        //循环遍历询价备件，加进其表
        List<InquirySpareVo> inquirySpareCodeVoList =inquiryAddandUpdateVo.getInquiryDetailsList();
        for (InquirySpareVo inquirySpareCodeVo : inquirySpareCodeVoList){
            inquirySpareCodeVo.setInquiryId(inquiryTotal.getId());
        }
        inquiryMapper.deleteSpareByTotalId(inquiryAddandUpdateVo.getId());
        inquiryMapper.addInquirysparedetail(inquirySpareCodeVoList);
        //循环遍历询价供应商，加进其表
        List<InquirySupplierVo> inquirySupplierVoList =inquiryAddandUpdateVo.getSupplierDetailsList();
        for (InquirySupplierVo inquirySupplierVo : inquirySupplierVoList){
            inquirySupplierVo.setInquiryId(inquiryTotal.getId());
        }
        inquiryMapper.deleteSupplierByTotalId(inquiryAddandUpdateVo.getId());
        inquiryMapper.addInquirysupplierdetail(inquirySupplierVoList);
        return ResultBuilderVo.success();
    }

    //选择备件
    @Override
    public ResultVo selectMaterial(SelectSpareVo selectSpareVo) {
        List<SpareVo> spareVoList = inquiryMapper.querySparePage(selectSpareVo);
        int count = inquiryMapper.querySparePageCount(selectSpareVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(spareVoList);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(selectSpareVo.getPageIndex());
        allRecords.setPageSize(selectSpareVo.getPageSize());
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    //选择供应商
    @Override
    public ResultVo searchPartnerPage(SelectPartnerVo selectPartnerVo) {
        AllRecords allRecords = new AllRecords();
        int count = inquiryMapper.queryPartnerPageCount(selectPartnerVo);
        List<PartnerVo> partnerVoList = inquiryMapper.queryPartnerPage(selectPartnerVo);
        allRecords.setDataList(partnerVoList);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(selectPartnerVo.getPageIndex());
        allRecords.setPageSize(selectPartnerVo.getPageSize());
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }
    //导入
    @Override
    public ResultVo inquiryIntroduction(MultipartFile file)throws Exception {
        //1.给用户提供导入模板
        //2.提示版本问题，询问产品经理支持哪种版本的导入格式，使用2007版本的导入模板，根据版本不同，poi导入技术的读取方式不同
        if (!file.getOriginalFilename().contains("xlsx")) {
            return ResultBuilderVo.error(ErrorMessage.VERSIONIS);
        }
        ResultVo<List<InquirySpareVo>> resultVo = checkVersionAndFormat(file);
        if (!resultVo.getSuccess()) {
            return resultVo;
        }
        ResultVo<List<InquirySpareVo>> resultVo1 = checkData(resultVo.getData());
        if (!resultVo1.getSuccess()) {
            return resultVo1;
        }
        ResultVo resultVo2=checkSpareCoding(resultVo.getData());
        if (!resultVo2.getSuccess()) {
            return resultVo2;
        }
        ResultVo resultVo3=checkSpareCoding2(resultVo.getData());
        if (!resultVo3.getSuccess()) {
            return resultVo3;
        }

        List<InquirySpareVo> inquirySpareVoList=new ArrayList<>();
        for(InquirySpareVo inquirySpareVo:resultVo1.getData()){
            inquirySpareVoList.add(inquirySpareVo);
        }
        return ResultBuilderVo.success(inquirySpareVoList);
    }

    private ResultVo checkSpareCoding2(List<InquirySpareVo> inquirySpareDetailVoList) {
        //7.验证数据是否存在也就是是否合法
        //备件是否存在
        List<String> spareCodeList = new ArrayList<>();
        for (InquirySpareVo inquirySpareCodeVo:inquirySpareDetailVoList){
            spareCodeList.add(inquirySpareCodeVo.getSpareCoding());
        }
        List<String> returnCodeList =  inquiryMapper.querySpareByCodeList(spareCodeList);
        if (spareCodeList.size()!=returnCodeList.size()){
            return ResultBuilderVo.error(ErrorMessage.SPAREVOISNULL);
        }
        spareCodeList.removeAll(returnCodeList);
        if (!spareCodeList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SPAREVOISNULLIS+spareCodeList);
        }
        return ResultBuilderVo.success(inquirySpareDetailVoList);
    }

    private ResultVo checkSpareCoding(List<InquirySpareVo> inquirySpareDetailVoList) {
        //8.验证数据是否重复
        List<String> spareCodeList = new ArrayList<>();
        for (InquirySpareVo inquirySpareCodeVo:inquirySpareDetailVoList){
            spareCodeList.add(inquirySpareCodeVo.getSpareCoding());
        }
        Set<String> setSpareCode = new HashSet<>(spareCodeList);
        if (spareCodeList.size()!=setSpareCode.size()){
            return ResultBuilderVo.error(ErrorMessage.SPARRISNOTTWO);
        }
        List<String> codeList = new ArrayList<>();
        List<String> chognfuList = new ArrayList<>();
        for (String code :spareCodeList){
            if (codeList.contains(code)){
                chognfuList.add(code);
            }else {
                codeList.add(code);
            }
        }
        if (!chognfuList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SPARRISNOTTWOIS+chognfuList);
        }
        return ResultBuilderVo.success();
    }
    public ResultVo<List<InquirySpareVo>> checkVersionAndFormat(MultipartFile file) throws Exception{
        //3.模板是否是需要导入的模板，验证模板是否正确
        //poi核心类，用来读取excel表格中的数据
        XSSFWorkbook workbook=new XSSFWorkbook(file.getInputStream());
        //获取单元格中的信息，0即为sheet1
        XSSFSheet sheet=workbook.getSheetAt(0);
        //验证第一行第一列的表头信息是否为“询价备件模版”，若不是，提示错误
        if (!String.valueOf(sheet.getRow(sheet.getFirstRowNum()).getCell(0)).equals("询价备件")) {
            return ResultBuilderVo.error(ErrorMessage.FORMWORKWRONG);
        }
        List<InquirySpareVo> inquirySpareCodeVoList=new ArrayList<>();
        //遍历表格中的数据，从第五行开始读取，即标题下一行，没有数据时终止遍历
        for (int i = sheet.getFirstRowNum() + 4; i <= sheet.getLastRowNum(); i++) {
            //读取第一行数据
            XSSFRow row = sheet.getRow(i);
            if (row != null) {
                String test1 = String.valueOf(row.getCell(1)).trim();
                String test2 = String.valueOf(row.getCell(2)).trim();
                String test3 = String.valueOf(row.getCell(3)).trim();
                String test4 = String.valueOf(row.getCell(4)).trim();
                String test5 = String.valueOf(row.getCell(5)).trim();
                String test6 = String.valueOf(row.getCell(6)).trim();
                String test7 = String.valueOf(row.getCell(7)).trim();
                //判断存入的数据不能为''和null
                if (!test1.equals("") && !test1.equals("null")
                        || !test2.equals("") && !test1.equals("null")
                        || !test3.equals("") && !test1.equals("null")
                        || !test4.equals("") && !test1.equals("null")
                        || !test5.equals("") && !test1.equals("null")
                        || !test6.equals("") && !test1.equals("null")
                        || !test7.equals("") && !test1.equals("null")
                      ) {
                    //把每一行的数据放入到实体类中
                    ResultVo<InquirySpareVo> inquiryImportVo = build(row);
                  /*  if (!inquiryImportVo.getSuccess()) {
                       return inquiryImportVo;
                    }*/
                    //把实体类中的数据放入到list集合中
                    inquirySpareCodeVoList.add(inquiryImportVo.getData());
                }
            }
        }
        return ResultBuilderVo.success(inquirySpareCodeVoList);
    }

    private ResultVo<InquirySpareVo> build(XSSFRow row) {
        InquirySpareVo inquirySpareVo = new InquirySpareVo();
        //4.格式问题，必须保持统一格式，统一为字符串格式导入
        try {
            inquirySpareVo.setSpareCoding(String.valueOf(row.getCell(1)));
        } catch (Exception e) {
            return ResultBuilderVo.error(ErrorMessage.PLEASECHECKCODEFORMAT);
        }
        inquirySpareVo.setSpareName(String.valueOf(row.getCell(2)));
        inquirySpareVo.setMeasurementUnit(String.valueOf(row.getCell(3)));
        inquirySpareVo.setInquiryQty(Integer.valueOf(String.valueOf(row.getCell(4))));
        inquirySpareVo.setCurrencyCodeName((String.valueOf(row.getCell(5))));
        inquirySpareVo.setMoq(Integer.valueOf(String.valueOf(row.getCell(6))));
        inquirySpareVo.setDeliveryCycle(Integer.valueOf(String.valueOf(row.getCell(7))));
        return ResultBuilderVo.success(inquirySpareVo);
    }

    public ResultVo checkData( List<InquirySpareVo> inquirySpareDetailVoList){
//      5.验证不为空字段是否为空
//      6.验证字符长度是否符合需求

        for (InquirySpareVo inquirySpareCodeVo:inquirySpareDetailVoList){
            if (inquirySpareCodeVo.getSpareCoding().isEmpty()){
                return ResultBuilderVo.error(ErrorMessage.SPARECODINGISNOT);
            }else{
                if (inquirySpareCodeVo.getSpareCoding().length()>50){
                    return ResultBuilderVo.error(ErrorMessage.SPARECODINGLENGTH);
                }
            }
            if (inquirySpareCodeVo.getSpareName().isEmpty()){
                return ResultBuilderVo.error(ErrorMessage.SPARENAMEISNOT);
            }else{
                if (inquirySpareCodeVo.getSpareName().length()>50){
                    return ResultBuilderVo.error(ErrorMessage.PLANCLASSADDCODELENGTH);
                }
            }

        }
        

        //9.对于需要转换的数据进行数据转换，比如数据库存的数据为数字格式，用户输入的为汉字，需要把汉字转换为数字存储
        for (InquirySpareVo inquirySpareVo : inquirySpareDetailVoList) {
            String currencyCodeName = inquirySpareVo.getCurrencyCodeName();
            if (currencyCodeName.contains("人民币")){
                CurrencyCodeEnum.getIndex(currencyCodeName);
            } if (currencyCodeName.contains("美元")){
                CurrencyCodeEnum.getIndex(currencyCodeName);
            } if (currencyCodeName.contains("英镑")){
                CurrencyCodeEnum.getIndex(currencyCodeName);
            }
        }

        return ResultBuilderVo.success(inquirySpareDetailVoList);
    }

    public  ResultVo validateInquiry(InquiryAddandUpdateVo inquiryAddandUpdateVo){
        /**
         *  询价类型是否存在
         询价币别是否存在
         是否含税正确
         开始时间是否在结束之前
         */
        if (StringUtils.isEmpty(TypeEnum.getName(inquiryAddandUpdateVo.getType()))){
            return ResultBuilderVo.error(ErrorMessage.TYPEISNULL);
        }
        if (StringUtils.isEmpty(CurrencyCodeEnum.getName(inquiryAddandUpdateVo.getCurrencyCode()))){
            return ResultBuilderVo.error(ErrorMessage.CURRENCYCODEISNULL);
        }
        if (StringUtils.isEmpty(IsTaxEnum.getName(inquiryAddandUpdateVo.getIsTax()))){
            return ResultBuilderVo.error(ErrorMessage.ISTAXISNULL);
        }
        int res=inquiryAddandUpdateVo.getStartTime().compareTo(inquiryAddandUpdateVo.getEndTime());
        if(res>0){
            return ResultBuilderVo.error(ErrorMessage.STARTISEND);
        }
        return ResultBuilderVo.success();
    }

    public ResultVo validateInquiry2(InquiryAddandUpdateVo inquiryAddandUpdateVo){
        //备件不能重复
        List<String> spareCodeList = new ArrayList<>();
        List<InquirySpareVo> inquirySpareCodeVoList = inquiryAddandUpdateVo.getInquiryDetailsList();
        for (InquirySpareVo inquirySpareCodeVo:inquirySpareCodeVoList){
            spareCodeList.add(inquirySpareCodeVo.getSpareCoding());
        }
        Set<String> setSpareCode = new HashSet<>(spareCodeList);
        if (spareCodeList.size()!=setSpareCode.size()){
            return ResultBuilderVo.error(ErrorMessage.SPARRISNOTTWO);
        }
        List<String> codeList = new ArrayList<>();
        List<String> chognfuList = new ArrayList<>();
        for (String code :spareCodeList){
            if (codeList.contains(code)){
                chognfuList.add(code);
            }else {
                codeList.add(code);
            }
        }
        if (!chognfuList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SPARRISNOTTWOIS+chognfuList);
        }
        //供应商不能重复
        List<String> supplierCodeList = new ArrayList<>();
        List<InquirySupplierVo> inquirySupplierVoList = inquiryAddandUpdateVo.getSupplierDetailsList();
        for (InquirySupplierVo inquirySupplierVo:inquirySupplierVoList){
            supplierCodeList.add(inquirySupplierVo.getVendorCode());
        }
        Set<String> setSupplierCode = new HashSet<>(supplierCodeList);
        if (supplierCodeList.size()!=setSupplierCode.size()){
            return ResultBuilderVo.error(ErrorMessage.SUPPLIERISNOTTWO);
        }
        List<String> sucodeList = new ArrayList<>();
        List<String> suchongfuList = new ArrayList<>();
        for (String code :supplierCodeList){
            if (sucodeList.contains(code)){
                suchongfuList.add(code);
            }else {
                sucodeList.add(code);
            }
        }
        if (!suchongfuList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SUPPLIERISNOTTWOIS+suchongfuList);
        }
        return ResultBuilderVo.success(inquiryAddandUpdateVo);
    }

    public ResultVo validateInquiry4(InquiryAddandUpdateVo inquiryAddandUpdateVo){
          /*1.验证备件是否有可以生产的供应商
        2.验证供应商是否存在可以生产他的备件*/
        List<InquirySpareVo> inquirySpareCodeVoList = inquiryAddandUpdateVo.getInquiryDetailsList();
        List<InquirySupplierVo> inquirySupplierVoList = inquiryAddandUpdateVo.getSupplierDetailsList();
        //前端传过来的供应商集合

        List<String> supplierCodeList = new ArrayList<>();
        for (InquirySupplierVo inquirySupplierVo:inquirySupplierVoList){
            supplierCodeList.add(inquirySupplierVo.getVendorCode());
        }
        List<String> codeList = new ArrayList<>();
        for (InquirySpareVo inquirySpareVo: inquirySpareCodeVoList){
            codeList.add(inquirySpareVo.getSpareCoding());
        }
        //根据多个备件集合查询中间表信息，查看这些备件都有哪些供应商生产。
        List<InquirySpareSupplierVo> inquirySpareSupplierVoList =  inquiryMapper.querySpareSupplier(codeList);
        for (String code:codeList){
            //存放的就是每个备件的供应商
            List<String> returnSuppliserCodeList = new ArrayList<>();
            for (InquirySpareSupplierVo inquirySpareSupplierVo:inquirySpareSupplierVoList){
                if (code.equals(inquirySpareSupplierVo.getSpareCoding())){
                    returnSuppliserCodeList.add(inquirySpareSupplierVo.getVendorCode());
                }
            }
            //true不存在交集  false 代表存在交集
            if (Collections.disjoint(supplierCodeList,returnSuppliserCodeList)){
                return ResultBuilderVo.error("备件"+code+"没有合法的供应商，它合法的供应商是"+returnSuppliserCodeList);
            }
        }
        return ResultBuilderVo.success(inquiryAddandUpdateVo);
    }

    public ResultVo validateInquiry3(InquiryAddandUpdateVo inquiryAddandUpdateVo){
         /*
        法人是否存在
        供应商是否存在
        备件是否存在
            */
        // 1.验证法人是否存在
        int count = inquiryMapper.queryLegalPersonById(inquiryAddandUpdateVo.getLegalPersonId());
        if (count<=0){
            return ResultBuilderVo.error(ErrorMessage.LEGALPERSONVOISNULL);
        }
        //备件是否存在
        List<String> codeList = new ArrayList<>();
        for (InquirySpareVo inquirySpareVo:inquiryAddandUpdateVo.getInquiryDetailsList()){
            codeList.add(inquirySpareVo.getSpareCoding());
        }
        List<String> returnCodeList =  inquiryMapper.querySpareByCodeList(codeList);
        if (codeList.size()!=returnCodeList.size()){
            return ResultBuilderVo.error(ErrorMessage.SPAREVOISNULL);
        }
        codeList.removeAll(returnCodeList);
        if (!codeList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SPAREVOISNULLIS+codeList);
        }
        //供应商是否存在
        //新建集合存储所有供应商编码
        List<String> supplierCodeList = new ArrayList<>();
        for (InquirySupplierVo inquirySupplierVo:inquiryAddandUpdateVo.getSupplierDetailsList()){
            supplierCodeList.add(inquirySupplierVo.getVendorCode());
        }
        List<String> returnSupplierCodeList =  inquiryMapper.querySupplierBySupplierCodeList(supplierCodeList);
        if (supplierCodeList.size()!=returnSupplierCodeList.size()){
            return ResultBuilderVo.error(ErrorMessage.SUPPLIERISNULL);
        }
        supplierCodeList.removeAll(returnSupplierCodeList);
        if (!supplierCodeList.isEmpty()){
            return ResultBuilderVo.error(ErrorMessage.SUPPLIERISNULLIS+supplierCodeList);
        }
        return ResultBuilderVo.success(inquiryAddandUpdateVo);
    }
}
